Ext.define('appmod.controller.postcfs.Startup', {
    extend: 'Ext.app.Controller',

    views: [
    	//'mycfs.DefaultListView',
		'postcfs.MycfsForm',
		//'mycfs.MycfsEditingForm'
    ],

	models : [
		'mycfs.Mycfs'
	],
	
    stores: [
		//'mycfs.Paging'
	],	

    refs: [
		{ref: 'formView',selector:'mycfsform'}
		//{ref: 'searchListView',selector:'defaultlistview searchfield'}
		

	],

    init: function() {

    	this.control({

			'mycfsform': {
				addAttchment:{
					fn:this.addAttchment
				}
			}
						

    	});


    },
	
		
	loadInitComp : function() {
		var mainPanel = Ext.create('appmod.view.postcfs.MycfsForm' , {
			region : 'center',
			border:false,
			padding:'0 0 0 0'
		});
		return mainPanel;		
	},
	
	onLaunch:function(app) {
		var me = this;
		var formView = me.getFormView();
		var form = formView;
		
		
		var saveBtn = formView.down('button[action="addUserAccount"]');
		saveBtn.on("click",me.saveNewMycfs , me , {
			window : formView
		});
		
//		var addAttchmentBtn = formView.down('button[action="addAttachment"]');
//		addAttchmentBtn.on("click", me.addAttchment , me , {
//			window : formView,
//			formPanel : form
//		});
	},
	
	addAttchment:function(eOpts) {
		var me = this;
		
		var upWin = eOpts.window;
		var formPanel = eOpts.formPanel;
		var attchmentLayout = formPanel.down('fieldcontainer[name="attachmentLayout"]');
		var grid = formPanel.down('grid[name="attachmentGrid"]');
		//var data = grid.getStore().data;
		if(grid.getStore().data.length==0){
			var data = [{ 'name': eOpts.v,  "coller":"<a herf='#' >删除</a>"  }];
			var ds = Ext.create('Ext.data.Store', {
			    storeId:'simpsonsStore',
			    fields:['name', 'coller'],
			    data:data,
			    proxy: {
			        type: 'memory',
			        reader: {
			            type: 'json',
			            root: 'items'
			        }
			    }
			});
			grid.bindStore(ds);
			
		}else{
			grid.getStore().add({ 'name': eOpts.v,  "coller":"lisa@simpsons.com"  });
		}
		grid.addListener('cellclick', me.deleteAttachment);
		
	},
	
	deleteAttachment: function(AdviceGrid, b, columnIndex, e, a, rowIndex){
		//alert(rowIndex);
//				alert(AdviceGrid.getStore().getAt(0).get('name'));

				AdviceGrid.getStore().removeAt(rowIndex);
	},
	
	addAttchment2: function(btn , event , eOpts) {
		var xp=new Ext.XTemplate("<input type=file id=fl style='display:none'>"); 
		var upWin = eOpts.window;
		var formPanel = eOpts.formPanel;
		var attchmentLayout = formPanel.down('fieldcontainer[name="attachmentLayout"]');
		attchmentLayout.add({
					xtype:'fieldcontainer',
					layout: 'hbox',
					margin: '10 0 0 0',
					items: [
						{
						xtype: 'filefield',
	                    name: 'attachment',
	                    anchor: '95%'
						},
						{
							xtype: 'button',
							text:'delete',
							listeners: {
						        click: function() {
									attchmentLayout.remove(this.ownerCt);
						            
						        }
						    }
						}
					]
                    
                });
		formPanel.doLayout();   
	},
	
	saveNewMycfs:function(btn , event , eOpts) {
		var me = this;
//		var listView = me.getDefaultListView();
//		var dsUser = listView.getStore();

//		var upWin = eOpts.window;
		
		var formPanel = eOpts.window;;
		//var win = upWin;
		
		var form = formPanel.getForm();
		form.submit({
			url:'../../data/resource/provider/mycfs/submit_adding.json',
			//url:'http://www.baidu.com',
			success:function(form , action) {
				 // --- reflash store ---
				 //win.close();
				 
				 //dsUser.load();
				 
				 Ext.Msg.alert('成功', action.result.msg);
			},
			failure: function(form, action){
				switch (action.failureType) {
					case Ext.form.action.Action.CLIENT_INVALID:
						Ext.Msg.alert('失败', '表单里包含错误的信息');
						break;
					case Ext.form.action.Action.CONNECT_FAILURE:
						Ext.Msg.alert('失败', '服务器连接失败,请联系管理人员');
						break;
					case Ext.form.action.Action.SERVER_INVALID:
						Ext.Msg.alert('失败', action.result.msg);
				}
			}
		});
	}
	
	
	
});